package com.im.partchat.db.dao;

import com.im.partchat.App;
import com.im.partchat.bean.User;
import com.im.partchat.bean.User_Table;
import com.im.partchat.db.PartChatDataBase;
import com.raizlabs.android.dbflow.config.DatabaseDefinition;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.OperatorGroup;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.FastStoreModelTransaction;
import com.raizlabs.android.dbflow.structure.database.transaction.Transaction;

import java.util.List;

/**
 * Created by hhz on 2017/6/29.
 */
public class UserDao {

    public static final UserDao userDao = new UserDao();

    private UserDao(){}

    public static UserDao getInstance() {
        return userDao;
    }

    public void saveAll(List<User> users) {
//        FlowManager.getModelAdapter(User.class).saveAll(users);
        Delete.table(User.class);
        DatabaseDefinition database = FlowManager.getDatabase(PartChatDataBase.class);
        FastStoreModelTransaction<User> fastStoreModelTransaction = FastStoreModelTransaction
                .insertBuilder(FlowManager.getModelAdapter(User.class))
                .addAll(users)
                .build();
        Transaction transaction = database.beginTransactionAsync(fastStoreModelTransaction).build();
        transaction.execute();
    }

    /**
     * 根据Id获取数据
     * @param userId
     * @return
     */
    public User getUserById(String userId) {
        return SQLite.select().from(User.class)
                .where(User_Table.user_id.eq(userId))
                .querySingle();
    }

    /**
     * 根据关键字查询数据 手机号、账号、姓名
     * @param key
     * @return
     */
    public List<User> getUserByKey(String key) {
        return SQLite.select().from(User.class)
                .where(User_Table.user_id.isNot(App.getUserBean().getId()))
                .and(OperatorGroup.clause()
                        .or(User_Table.phone.like(key))
                        .or(User_Table.name.like(key))
                        .or(User_Table.jp.like(key)))
                .queryList();
    }

    /**
     * 用户ID在列表中所有用户
     * @param ids
     * @return
     */
    public List<User> getUsersInIds(List<String> ids) {
        return SQLite.select().from(User.class)
                .where(User_Table.user_id.in(ids))
                .queryList();
    }

    /**
     * 根据单位获取人员信息
     * @param deptId
     * @return
     */
    public List<User> getUserByDeptId(String deptId) {
        return SQLite.select().from(User.class)
                .where(User_Table.deptId.eq(deptId))
                .and(User_Table.user_id.isNot(App.getUserBean().getId()))
                .queryList();
    }

    /**
     * 获取好友列表
     * @return
     */
    public List<User> getFriends() {
        return SQLite.select().from(User.class)
                .where(User_Table.friends.eq(1))
                .and(User_Table.user_id.isNot(App.getUserBean().getId()))
                .queryList();
    }

    public void updateUser(String userId) {
//        User user = getUserById(userId);
//        user.setFriends(1);
//        FlowManager.getModelAdapter(User.class).update(user);
        SQLite.update(User.class)
                .set(User_Table.friends.eq(1))
                .where(User_Table.user_id.eq(userId))
                .execute();
    }
}
